import {ref } from "vue";
import useInstance from "@/hooks/useInstance";
export const useUserAddAndEdit = (dialog,onClose,onShow,addUserModel,emit) => {
  
  const {global} = useInstance()
  const addUserForm = ref()
  const confirm =()=>{
    addUserForm.value.validate((valid) => {
      if (valid) {
        // 调用父组件的save方法
        emit('save', addUserModel)
        onClose()
      }else {
        console.log('error submit!')
        return false
      }
    })
  }
   
  const show = (type,row)=>{
    console.log('----------show------------',row);
    // 修改Dialog的高度
    dialog.height = 200
    // 根据操作修改dialog标题
    type==='add'?dialog.title = '新增用户' : dialog.title = '编辑用户';
    onShow()
    // 清空表单信息
    Object.keys(addUserModel).forEach(key => {
      addUserModel[key] = ''
    })
    if(type ===  'edit'){
      Object.assign(addUserModel,row)
   }
    addUserModel.action= type
  }
  return{
    show,
    confirm,
    addUserForm
  }
}